package com.e2oto.seller.modules.seller.presenter;

import com.alibaba.fastjson.JSONObject;
import com.e2oto.seller.common.EasyAES;
import com.e2oto.seller.common.base.BaseBean;
import com.e2oto.seller.common.base.BasePresenter;
import com.e2oto.seller.common.base.LoadingDialog;
import com.e2oto.seller.common.http.ProgressDialogHandler;
import com.e2oto.seller.common.utils.RetrofitUtils;
import com.e2oto.seller.modules.SellerApp;
import com.e2oto.seller.modules.launch.bean.CodeBean;
import com.e2oto.seller.modules.launch.bean.LoginBean;
import com.e2oto.seller.modules.seller.bean.ApplyAdmissionCommitBean;
import com.e2oto.seller.modules.seller.views.ShopAptitudeView;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * Created by ZhangLiQiang on 2017/11/14.
 */

public class ShopAptitudePresenter extends BasePresenter<ShopAptitudeView> {

    public void applySeller(ApplyAdmissionCommitBean commitBean) {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user_id", commitBean.getUser_id());
        jsonObject.put("seller_name", commitBean.getSeller_name());
        jsonObject.put("seller_type", commitBean.getSeller_name());
        jsonObject.put("select_retail", commitBean.getSelect_retail());
        jsonObject.put("select_wholesale", commitBean.getSelect_wholesale());
        jsonObject.put("category_id", commitBean.getCategory_id());
        jsonObject.put("area_tree", commitBean.getArea_tree());
        jsonObject.put("spokesman", commitBean.getSpokesman());
        jsonObject.put("batch_category_id", commitBean.getBatch_category_id());
        String param = EasyAES.encrypt(jsonObject);

        MultipartBody.Builder builder = new MultipartBody.Builder().setType(MultipartBody.FORM)
                .addFormDataPart("paper_license", commitBean.getPaper_license().getName(), RequestBody.create(MediaType.parse("image/jpeg"), commitBean.getPaper_license()))
                .addFormDataPart("card_id_front", commitBean.getCard_id_front().getName(), RequestBody.create(MediaType.parse("image/jpeg"), commitBean.getCard_id_front()))
                .addFormDataPart("card_id_back", commitBean.getCard_id_back().getName(), RequestBody.create(MediaType.parse("image/jpeg"), commitBean.getCard_id_back()))
                .addFormDataPart("other_photo", commitBean.getOther_photo().getName(), RequestBody.create(MediaType.parse("image/jpeg"), commitBean.getOther_photo()))
                .addFormDataPart("param", param)
                .addFormDataPart("ver", SellerApp.versionName);

        final LoadingDialog loadingDialog = new LoadingDialog(getContext());
        RetrofitUtils.getApiService().applySeller(builder.build())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseBean<LoginBean>>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        loadingDialog.show();
                    }

                    @Override
                    public void onNext(BaseBean<LoginBean> baseBean) {
                        getView().applySellerCallback(baseBean);
                    }

                    @Override
                    public void onError(Throwable e) {
                        getView().showMessage(e.toString());
                        loadingDialog.dismiss();
                    }

                    @Override
                    public void onComplete() {
                        loadingDialog.dismiss();
                    }
                });
    }
}
